class SingleLinkedList {
    //定义一个类部类：因为链表独特的属性分为两个部分，一个为存下一个地址，一个为本地址所存内容
    static class ListNode {
        public int val;
        public ListNode next;//存放本元素下个元素对应地址

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//链表的头节点

    public void creat() {
        ListNode Node1 = new ListNode(12);
        ListNode Node2 = new ListNode(13);
        ListNode Node3 = new ListNode(14);
        ListNode Node4 = new ListNode(15);
        ListNode Node5 = new ListNode(17);
        ListNode Node6 = new ListNode(17);

        Node1.next = Node2;
        Node2.next = Node3;
        Node3.next = Node4;
        Node4.next = Node5;
        Node5.next = Node6;
        this.head = Node1;
    }

    //头插法
    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);
        newNode.next = head;
        head = newNode;
    }

    //尾插法
    public void addLast(int data) {
        ListNode newNode = new ListNode(data);
        if (head == null) {
            head = newNode; // 链表为空，新节点成为头节点
        } else {
            ListNode current = head;
            while (current.next != null) {
                current = current.next; // 遍历到链表的最后一个节点
            }
            current.next = newNode; // 将新节点添加到链表末尾
        }
    }

    //任意位置插入,
    public void addIndex(int index, int data) {
        ListNode newNode = new ListNode(data);
        if(index<0||index>=size()){
            System.out.println("不合法");
            return;
        }if (index==0) {
            addFirst(data);
        }if(index==size()){
            addLast(data);
        }
        ListNode cur=head;
        while (index-1!=0){
            cur=cur.next;
            index--;
        }
        newNode.next=cur.next;
        cur.next=newNode;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode temp=head;
        while (temp!=null){
            if(temp.val==key){
                return true;
            }
            temp=temp.next;
        }
        return false;
    }


    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        //如果链表为空返回
        if(head==null){
            return;
        }if(head.val==key){
            head=head.next;
            return;
        }//如果第一个值为key
        ListNode cur=findNodeKEY(key);
        if(cur==null){
            return;
        }
        ListNode del=cur.next;
        cur.next=del.next;
    }
    private ListNode findNodeKEY(int key){
        ListNode cur=head;
        while (cur!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(head==null){
            return;
        }
        ListNode code=head;
        ListNode cur=head.next;
        while(cur!=null){
            //连在一起
            if(cur.val==key){
                code.next=cur.next;
                cur=cur.next;
            }else{
                code=cur;
                cur=cur.next;
            }
        }

    }


    //得到单链表的长度
    public int size() {
        int len=0;
        ListNode cur=head;
        while(cur!=null){
            len++;
            cur=cur.next;
        }
        return len;
    }

    public void clear() {
        ListNode cur=head;
        while (cur!=null){
            ListNode cur1=cur.next;
            cur.next=null;
            cur=cur1;
        }
        head=null;
    }

    public void display() {
        ListNode cur=head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }
}
public class lianbiao {
    public static void main(String[] args) {
        SingleLinkedList list=new SingleLinkedList();
        list.creat();
        //list.addFirst(10);
        //list.addLast(20);
        //list.addIndex(3,6);
        //System.out.println(list.contains(15));
        //list.remove(15);
        list.removeAllKey(17);
        list.display();

    }
}